


$(function () {
    var el = $('#weather');
    var city = el.find('.city').eq(0);
    var date = el.find('.date').eq(0);
    var icon = el.find('.icon img').eq(0);
    var pm25 = el.find('.pm25 span').eq(0);
    var weather = el.find('.weather').eq(0);
    var temp = el.find('.temp strong').eq(0);
    var wind = el.find('.wind').eq(0);

    getWeatherData({}, function (res) {
        if (0 === res.error) {
            var result = res.results[0];
            if (result) {
                var data = result.weather_data;

                icon.attr('src', data[0].dayPictureUrl);
                city.html(result.currentCity);
                pm25.html(result.pm25);
                date.html(data[0].date);
                weather.html(data[0].weather);
                temp.html(data[0].temperature.replace('℃', ''));
                wind.html(data[0].wind);
            }
        }
    });
});


function getWeatherData (params, callback) {
    var ak = 'FBQHmMgH6siur3FGAGTxL3FX';
    var url = 'http://api.map.baidu.com/telematics/v3/weather';
    var params = {
        location: '杭州',
        output: 'json',
        ak: ak
    };
    $.ajax({
        url: url,
        dataType: 'jsonp',
        data: params,
        success: function (res) {
            console.log('success', res);
            if ('function' == typeof callback) {
                callback(res);
            }
        },
        error: function (err) {
            console.log('error', err);
        }
    });
}

// api.openweathermap.org/data/2.5/weather